package com.hecate.user.service.impl;

import com.hecate.common.bid.BidUtils;
import com.hecate.base.enums.StatusEnum;
import com.hecate.common.exception.HecateBusinessException;
import com.hecate.common.base.basedo.TableId;
import com.hecate.common.response.HecateResponse;
import com.hecate.common.response.HecateResponseCodeEnum;
import com.hecate.common.utils.StringUtils;
import com.hecate.user.api.req.permissionresource.DeletePermissionResourceByBidRequest;
import com.hecate.user.api.req.permissionresource.FindPermissionResourceByBidRequest;
import com.hecate.user.api.req.permissionresource.SavePermissionResourceRequest;
import com.hecate.user.api.req.permissionresource.UpdatePermissionResourceRequest;
import com.hecate.user.dto.permissionresource.DeletePermissionResourceByBidDTO;
import com.hecate.user.dto.permissionresource.FindPermissionResourceByBidDTO;
import com.hecate.user.dto.permissionresource.SavePermissionResourceDTO;
import com.hecate.user.dto.permissionresource.UpdatePermissionResourceDTO;
import com.hecate.user.persistence.mapper.PermissionResourceMapper;
import com.hecate.user.persistence.model.PermissionResource;
import com.hecate.user.service.PermissionResourceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 黄星星
 * @date 2020-12-05
 */
@Service
public class PermissionResourceServiceImpl implements PermissionResourceService {

    @Autowired
    private PermissionResourceMapper permissionResourceMapper;

    @Override
    public HecateResponse<SavePermissionResourceDTO> savePermissionResource(SavePermissionResourceRequest savePermissionResourceRequest) {
        String permissionCode = savePermissionResourceRequest.getPermissionCode();
        if(StringUtils.isEmpty(permissionCode)){
            throw new HecateBusinessException(HecateResponseCodeEnum.PARAM_IS_BLANK.getMessage());
//            return HecateResponse.ERROR(HecateResponseCodeEnum.PARAM_IS_BLANK,"权限code不能为空!");
        }
        String permissionName = savePermissionResourceRequest.getPermissionName();
        if(StringUtils.isEmpty(permissionName)){
            return HecateResponse.ERROR(HecateResponseCodeEnum.PARAM_IS_BLANK,"权限名称不能为空!");
        }

        PermissionResource permissionResource = new PermissionResource();
        BeanUtils.copyProperties(savePermissionResourceRequest, permissionResource);
        permissionResource.setBid(BidUtils.bid(TableId.U_PERMISSION_RESOURCE));
        permissionResourceMapper.insertSelective(permissionResource);
        return HecateResponse.SUCCESS();
    }

    @Override
    public HecateResponse<UpdatePermissionResourceDTO> updatePermissionResource(UpdatePermissionResourceRequest updatePermissionResourceRequest) {
        String bid = updatePermissionResourceRequest.getBid();
        if(StringUtils.isEmpty(bid)){
            return HecateResponse.ERROR(HecateResponseCodeEnum.PARAM_IS_BLANK,"权限资源标识不能为空!");
        }
        PermissionResource permissionResource = permissionResourceMapper.selectByBid(bid);
        if(null == permissionResource){
            return HecateResponse.ERROR(HecateResponseCodeEnum.PARAM_IS_BLANK,"权限资源记录不存在!");
        }
        // 资源权限code
        String permissionCode = updatePermissionResourceRequest.getPermissionCode();
        if(StringUtils.isEmpty(permissionCode)){
            permissionResource.setPermissionCode(permissionCode);
        }
        // 资源权限名称
        String permissionName = updatePermissionResourceRequest.getPermissionName();
        if(StringUtils.isEmpty(permissionName)){
            permissionResource.setPermissionName(permissionName);
        }
        // 上级资源权限
        String parentPermissionBid = updatePermissionResourceRequest.getParentPermissionBid();
        if(StringUtils.isEmpty(parentPermissionBid)){
            permissionResource.setParentPermissionBid(parentPermissionBid);
        }
        permissionResourceMapper.updateByPrimaryKeySelective(permissionResource);
        return HecateResponse.SUCCESS("更新权限资源成功!");
    }

    @Override
    public HecateResponse<DeletePermissionResourceByBidDTO> deletePermissionResourceByBid(DeletePermissionResourceByBidRequest deletePermissionResourceByBidRequest) {
        String bid = deletePermissionResourceByBidRequest.getBid();
        if(StringUtils.isEmpty(bid)){
            return HecateResponse.ERROR(HecateResponseCodeEnum.PARAM_IS_BLANK,"权限资源标识不能为空!");
        }
        PermissionResource permissionResource = permissionResourceMapper.selectByBid(bid);
        if(null != permissionResource){
            permissionResource.setStatus(StatusEnum.DISABLE.getCode());
        }
        permissionResourceMapper.updateByBid(permissionResource);
        return HecateResponse.SUCCESS("删除权限资源成功!");
    }

    @Override
    public HecateResponse<FindPermissionResourceByBidDTO> findPermissionResourceByBid(FindPermissionResourceByBidRequest findPermissionResourceByBidRequest) {
        String bid = findPermissionResourceByBidRequest.getBid();
        if(StringUtils.isEmpty(bid)){
            return HecateResponse.ERROR(HecateResponseCodeEnum.PARAM_IS_BLANK,"权限资源标识不能为空!");
        }
        PermissionResource permissionResource = permissionResourceMapper.selectByBid(bid);
        FindPermissionResourceByBidDTO findPermissionResourceByBidDTO = new FindPermissionResourceByBidDTO();
        BeanUtils.copyProperties(permissionResource, findPermissionResourceByBidDTO);
        return HecateResponse.SUCCESS(findPermissionResourceByBidDTO);
    }
}
